import random
import time

# 定义十个勤天成员
members = [
    "蒋敦豪", "鹭卓", "李耕耘", "李昊", "赵一博",
    "卓沅", "赵小童", "何浩楠", "陈少熙", "王一珩"
]

# 游戏状态
game_state = {
    "money": 100,
    "hunger": 50,
    "energy": 100,
    "friendship": {member: 50 for member in members},
    "crops": {},
    "animals": {
        "小鸡": {"hunger": 30, "health": 80},
        "小羊": {"hunger": 40, "health": 70},
        "小猪": {"hunger": 60, "health": 60}
    }
}

def print_slow(text, delay=0.05):
    """缓慢打印文本，增加游戏体验"""
    for char in text:
        print(char, end='', flush=True)
        time.sleep(delay)
    print()

def show_status():
    """显示当前游戏状态"""
    print("\n===== 状态 =====")
    print(f"金钱: {game_state['money']} 元")
    print(f"饥饿度: {game_state['hunger']}/100")
    print(f"能量: {game_state['energy']}/100")
    print("================\n")

def choose_member():
    """让玩家选择一个成员一起活动"""
    print("可以选择的成员:")
    for i, member in enumerate(members, 1):
        print(f"{i}. {member} (友好度: {game_state['friendship'][member]})")
    
    while True:
        try:
            choice = int(input("请选择一个成员(输入编号): ")) - 1
            if 0 <= choice < len(members):
                return members[choice]
            else:
                print("请输入有效的编号!")
        except ValueError:
            print("请输入数字!")

def feed_animals():
    """喂食小动物小游戏"""
    member = choose_member()
    print_slow(f"\n你和{member}一起来到动物棚，准备喂食小动物~")
    
    animal = random.choice(list(game_state['animals'].keys()))
    print_slow(f"今天要喂的是{animal}!")
    
    print_slow(f"{animal}现在的饥饿度是{game_state['animals'][animal]['hunger']}")
    print_slow("请选择喂食的量(1-10):")
    
    try:
        amount = int(input("> "))
        if 1 <= amount <= 10:
            game_state['animals'][animal]['hunger'] = max(0, game_state['animals'][animal]['hunger'] - amount * 5)
            game_state['animals'][animal]['health'] = min(100, game_state['animals'][animal]['health'] + amount * 2)
            game_state['friendship'][member] += amount
            game_state['energy'] -= amount * 3
            game_state['hunger'] += amount * 2
            
            print_slow(f"你和{member}成功喂了{animal}，它看起来很开心!")
            print_slow(f"现在{animal}的饥饿度是{game_state['animals'][animal]['hunger']}，健康度是{game_state['animals'][animal]['health']}")
            print_slow(f"你和{member}的友好度增加了{amount}点!")
        else:
            print_slow("喂食量必须在1-10之间哦!")
    except ValueError:
        print_slow("请输入有效的数字!")

def plant_crops():
    """种植农作物小游戏"""
    member = choose_member()
    print_slow(f"\n你和{member}来到田地，准备种植农作物~")
    
    crops = ["小麦", "玉米", "蔬菜", "水果"]
    print_slow("可以种植的农作物:")
    for i, crop in enumerate(crops, 1):
        print(f"{i}. {crop}")
    
    try:
        choice = int(input("请选择要种植的农作物(输入编号): ")) - 1
        if 0 <= choice < len(crops):
            crop = crops[choice]
            cost = 10 + choice * 5
            if game_state['money'] >= cost:
                game_state['money'] -= cost
                game_state['energy'] -= 15
                game_state['hunger'] += 10
                game_state['friendship'][member] += 8
                
                if crop not in game_state['crops']:
                    game_state['crops'][crop] = 0
                game_state['crops'][crop] += 1
                
                print_slow(f"你和{member}成功种下了{crop}! 花费了{cost}元")
                print_slow(f"等一段时间，{crop}就会成熟可以收获了~")
            else:
                print_slow("你的钱不够种植这个农作物哦!")
        else:
            print_slow("请输入有效的编号!")
    except ValueError:
        print_slow("请输入有效的数字!")

def cook_food():
    """做饭小游戏"""
    member = choose_member()
    print_slow(f"\n你和{member}来到厨房，准备做饭~")
    
    if not game_state['crops']:
        print_slow("哎呀，没有可以用来做饭的食材，先去种点农作物吧!")
        return
    
    print_slow("可以做的菜:")
    dishes = []
    if "蔬菜" in game_state['crops'] and game_state['crops']['蔬菜'] > 0:
        dishes.append("炒青菜")
    if "小麦" in game_state['crops'] and game_state['crops']['小麦'] > 0:
        dishes.append("馒头")
    if "玉米" in game_state['crops'] and game_state['crops']['玉米'] > 0:
        dishes.append("玉米汤")
    if "水果" in game_state['crops'] and game_state['crops']['水果'] > 0:
        dishes.append("水果沙拉")
    
    for i, dish in enumerate(dishes, 1):
        print(f"{i}. {dish}")
    
    try:
        choice = int(input("请选择要做的菜(输入编号): ")) - 1
        if 0 <= choice < len(dishes):
            dish = dishes[choice]
            print_slow(f"开始做{dish}啦，需要按照步骤来哦~")
            
            # 简单的做饭步骤小游戏
            steps = random.randint(3, 5)
            success = True
            for i in range(steps):
                print_slow(f"\n步骤{i+1}/{steps}: 请输入'{member}'的名字继续:")
                answer = input("> ")
                if answer != member:
                    print_slow(f"错啦! 是'{member}'哦，重新来~")
                    success = False
                    break
                print_slow("对啦! 继续下一步~")
            
            if success:
                print_slow(f"太棒了! 你和{member}成功做出了美味的{dish}!")
                game_state['hunger'] = max(0, game_state['hunger'] - 30)
                game_state['energy'] = min(100, game_state['energy'] + 20)
                game_state['friendship'][member] += 15
                
                # 消耗食材
                if dish == "炒青菜":
                    game_state['crops']['蔬菜'] -= 1
                elif dish == "馒头":
                    game_state['crops']['小麦'] -= 1
                elif dish == "玉米汤":
                    game_state['crops']['玉米'] -= 1
                elif dish == "水果沙拉":
                    game_state['crops']['水果'] -= 1
            else:
                print_slow(f"有点可惜，这次{member}不太满意... 下次加油!")
                game_state['friendship'][member] -= 5
        else:
            print_slow("请输入有效的编号!")
    except ValueError:
        print_slow("请输入有效的数字!")

def play_basketball():
    """打篮球小游戏"""
    member = choose_member()
    print_slow(f"\n你和{member}来到篮球场，准备来一场篮球赛~")
    
    print_slow("比赛开始! 请在3秒内输入'投球'来投篮:")
    start_time = time.time()
    success = False
    
    try:
        while time.time() - start_time < 3:
            action = input("> ")
            if action == "投球":
                # 随机决定是否投中
                if random.random() > 0.3:
                    print_slow("太棒了! 投中了!")
                    success = True
                else:
                    print_slow("哎呀，没投中...")
                break
        else:
            print_slow("时间到! 没投出去~")
        
        if success:
            game_state['friendship'][member] += 12
            game_state['energy'] -= 20
            game_state['hunger'] += 15
            print_slow(f"你和{member}配合得真默契! 友好度增加了12点!")
        else:
            game_state['friendship'][member] += 5
            game_state['energy'] -= 10
            game_state['hunger'] += 10
            print_slow(f"虽然没投中，但{member}说玩得很开心! 友好度增加了5点!")
    except:
        print_slow("游戏出错了，返回主菜单~")

def dance():
    """跳舞小游戏"""
    member = choose_member()
    print_slow(f"\n你和{member}来到舞台，准备跳舞~")
    print_slow("请跟随节奏输入对应的方向键(上/下/左/右):")
    
    score = 0
    directions = ["上", "下", "左", "右"]
    
    for _ in range(5):
        direction = random.choice(directions)
        print_slow(f"请输入'{direction}':")
        user_input = input("> ")
        
        if user_input == direction:
            print_slow("对啦! 节奏很棒!")
            score += 1
        else:
            print_slow(f"错啦! 应该是'{direction}'哦~")
    
    print_slow(f"\n舞蹈结束! 你的得分是{score}/5")
    
    if score >= 4:
        print_slow(f"太厉害了! 你和{member}跳得太棒了!")
        game_state['friendship'][member] += 20
    elif score >= 2:
        print_slow(f"不错哦! {member}对你的舞步很满意~")
        game_state['friendship'][member] += 10
    else:
        print_slow(f"继续练习吧! {member}相信你下次会更好~")
        game_state['friendship'][member] += 3
    
    game_state['energy'] -= 15
    game_state['hunger'] += 10

def harvest_crops():
    """收获农作物"""
    if not game_state['crops'] or all(count == 0 for count in game_state['crops'].values()):
        print_slow("没有可以收获的农作物哦!")
        return
    
    print_slow("\n可以收获的农作物:")
    harvestable = []
    for crop, count in game_state['crops'].items():
        if count > 0:
            harvestable.append(crop)
            print(f"- {crop}: {count}个")
    
    try:
        crop = input("请输入要收获的农作物名称: ")
        if crop in harvestable:
            amount = game_state['crops'][crop]
            value = amount * (10 + random.randint(1, 10))
            game_state['money'] += value
            game_state['crops'][crop] = 0
            print_slow(f"成功收获{amount}个{crop}，卖了{value}元!")
        else:
            print_slow("没有这个农作物可以收获哦!")
    except:
        print_slow("输入错误，返回主菜单~")

def main():
    """游戏主函数"""
    print_slow("欢迎来到勤天小镇!")
    print_slow("在这里，你可以和十个勤天的成员一起体验各种有趣的活动~")
    print_slow("让我们开始吧!\n")
    
    while True:
        # 检查状态是否过低
        if game_state['hunger'] >= 90:
            print_slow("\n你太饿了，需要做点东西吃!")
        if game_state['energy'] <= 10:
            print_slow("\n你太累了，需要休息一下!")
        
        print("\n===== 勤天小镇 =====")
        print("1. 喂食小动物")
        print("2. 种植农作物")
        print("3. 收获农作物")
        print("4. 做饭")
        print("5. 打篮球")
        print("6. 跳舞")
        print("7. 查看状态")
        print("8. 退出游戏")
        print("====================")
        
        try:
            choice = int(input("请选择要做的事情(输入编号): "))
            
            if choice == 1:
                if game_state['energy'] < 10:
                    print_slow("你太累了，没有力气喂食小动物了~")
                else:
                    feed_animals()
            elif choice == 2:
                if game_state['energy'] < 15:
                    print_slow("你太累了，没有力气种植农作物了~")
                else:
                    plant_crops()
            elif choice == 3:
                harvest_crops()
            elif choice == 4:
                if game_state['energy'] < 10:
                    print_slow("你太累了，没有力气做饭了~")
                else:
                    cook_food()
            elif choice == 5:
                if game_state['energy'] < 20:
                    print_slow("你太累了，没有力气打篮球了~")
                else:
                    play_basketball()
            elif choice == 6:
                if game_state['energy'] < 15:
                    print_slow("你太累了，没有力气跳舞了~")
                else:
                    dance()
            elif choice == 7:
                show_status()
            elif choice == 8:
                print_slow("\n感谢游玩勤天小镇! 下次再见~")
                break
            else:
                print_slow("请输入1-8之间的数字!")
            
            # 每次行动后随机事件
            if random.random() < 0.2:
                random_member = random.choice(members)
                change = random.randint(1, 5)
                game_state['friendship'][random_member] += change
                print_slow(f"\n惊喜! {random_member}对你的好感度增加了{change}点~")
            
            # 能量和饥饿度随时间变化
            game_state['energy'] = max(0, game_state['energy'] - 2)
            game_state['hunger'] = min(100, game_state['hunger'] + 2)
            
            time.sleep(1)
        except ValueError:
            print_slow("请输入有效的数字!")
        except Exception as e:
            print_slow(f"游戏出错了: {e}")
            break

if __name__ == "__main__":
    main()
